<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />

<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="author" content="Kevin Wright" />

<meta name="date" content="2018-05-24" />

<title>Overview of the pals package</title>



<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>



<link href="data:text/css;charset=utf-8,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23header%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0Aa%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%20code%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%0A" rel="stylesheet" type="text/css" />

</head>

<body>




<h1 class="title toc-ignore">Overview of the ‘pals’ package</h1>
<h4 class="author"><em>Kevin Wright</em></h4>
<h4 class="date"><em>2018-05-24</em></h4>



<style type="text/css">
body, td { font-size: 14pt; color: #000; padding:1em; }
code, pre, #ws, #message { font-size: 12pt;
  color: #000; background-color: #f5f5f5; }
code {border: 1px solid #e0e0e0; padding: 0 5px;}
pre > code { border: 0; }
</style>
<p>The purpose of the <code>pals</code> package is twofold:</p>
<ol style="list-style-type: decimal">
<li>Provide a comprehensive collection of color palettes and colormaps</li>
<li>Provide tools for evaluating these collections.</li>
</ol>
<p>Memory use is reduced by compressing colormaps to fewer colors and by calling <code>colorRampPalette</code> only when a colormap is requested.</p>
<p>This report gives some suggestions/recommendations for color and then gives an example of each evaluation tool.</p>
<div id="recommendations" class="section level1">
<h1>Recommendations</h1>
<p>The appearance of color depends on:</p>
<ol style="list-style-type: decimal">
<li>The display device (screen, paper, photocopy).</li>
<li>The type of graphic (regions/lines).</li>
<li>The person (age, gender, colorblindness)</li>
</ol>
<p>It is difficult to give definitive recommendations for the best palettes and colormaps. Nonetheless, here are some we like.</p>
<p>Diverging: <code>coolwarm</code>/<code>warmcool</code> avoid Mach banding in the middle.</p>
<p>Sequential (multi-hue): <code>ocean.haline</code>, <code>parula</code> (default in Matlab).</p>
<p>Sequential (one hue): <code>brewer.blues</code>.</p>
<p>Rainbow: <code>cubicl</code>, <code>kovesi.rainbow</code>.</p>
<p>Cyclical: <code>ocean.phase</code>.</p>
<p>Categorical: <code>brewer.paired</code>, <code>stepped</code></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">require</span>(pals)
<span class="kw">pal.bands</span>(coolwarm, parula, ocean.haline, brewer.blues, cubicl, kovesi.rainbow, ocean.phase, <span class="kw">brewer.paired</span>(<span class="dv">12</span>), stepped,
          <span class="dt">main=</span><span class="st">&quot;Colormap suggestions&quot;</span>)</code></pre></div>
<pre><code>## Only 24 colors are available with 'stepped'</code></pre>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
<div id="functions-in-the-pals-package" class="section level1">
<h1>Functions in the <code>pals</code> package</h1>
<div id="pal.bands" class="section level2">
<h2>pal.bands()</h2>
<p>Show palettes and colormaps as colored bands</p>
<div id="what-to-look-for" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>A good discrete palette has distinct colors.</li>
<li>A good continuous colormap does not show boundaries between colors.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">labs=<span class="kw">c</span>(<span class="st">'alphabet'</span>,<span class="st">'alphabet2'</span>, <span class="st">'glasbey'</span>,<span class="st">'kelly'</span>,<span class="st">'polychrome'</span>, <span class="st">'stepped'</span>, <span class="st">'tol'</span>, <span class="st">'watlington'</span>)
op=<span class="kw">par</span>(<span class="dt">mar=</span><span class="kw">c</span>(<span class="dv">0</span>,<span class="dv">5</span>,<span class="dv">3</span>,<span class="dv">1</span>))
<span class="kw">pal.bands</span>(<span class="kw">alphabet</span>(), <span class="kw">alphabet2</span>(), <span class="kw">glasbey</span>(), <span class="kw">kelly</span>(),
  <span class="kw">polychrome</span>(), <span class="kw">stepped</span>(), <span class="kw">tol</span>(), <span class="kw">watlington</span>(), <span class="dt">labels=</span>labs, <span class="dt">show.names=</span><span class="ot">FALSE</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">par</span>(op)
<span class="kw">pal.bands</span>(coolwarm, viridis, parula, <span class="dt">n=</span><span class="dv">200</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.channels" class="section level2">
<h2>pal.channels()</h2>
<p>Show the amount of red, green, blue, and gray in colors of a palette. The gray line corresponds to luminosity.</p>
<div id="what-to-look-for-1" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Sequential data should usually be shown with a colormap that is smoothly increasing in lightness, as shown by the gray line.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.channels</span>(parula, <span class="dt">main=</span><span class="st">&quot;parula&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.cluster" class="section level2">
<h2>pal.cluster()</h2>
<p>Show a palette with heirarchical clustering</p>
<p>The palette colors are converted to LUV coordinates before clustering.</p>
<div id="what-to-look-for-2" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Colors that are visually similar tend to be clustered together.</li>
<li>Leaves of the dendrogram should not be at substantially different heights.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.cluster</span>(<span class="kw">alphabet2</span>(), <span class="dt">main=</span><span class="st">&quot;alphabet2&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.compress" class="section level2">
<h2>pal.compress()</h2>
<p>A few colormaps in the <code>pals</code> package are defined with mathematical formulas (e.g. the cubehelix colormap), but most of the colormaps are originally defined as a smooth curve through a seqence of 256 colors. There seems to be no theoretical reason for 256 colors, other than tradition. It is natural to wonder if a smooth curve through fewer colors would be equally sufficient. This function compresses a colormap function down to a small-ish vector of colors that can be passed into <code>colorRampPalette</code> to re-create the original palette with a non-noticeable difference. Most of the palettes in the <code>pals</code> package are stored as a compressed sequence of colors.</p>
<p>How effective is <code>pal.compress</code>? Compressing all 50 <code>kovesi.*</code> colormaps reduced memory from 352000 to 46000 bytes, a savings of 87%.</p>
<p>In the figure below, the top band is the (mathematically-defined) <code>cubehelix</code> colormap function evaluated at 255 colors. The <code>cubebasis</code> vector has 17 colors (shown in the middle). These 17 colors are passed into the <code>colorRampPalette</code> function and expanded to 255 colors shown in the bottom band. The maximum squared LUV distance between the individual colors in the two bands is 2.34, which is smaller than the theoretical perceptual difference of roughly 2.5.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># smooth palettes are usually easy to compress</span>
p1 &lt;-<span class="st"> </span><span class="kw">cubehelix</span>(<span class="dv">255</span>)
cubebasis &lt;-<span class="st"> </span><span class="kw">pal.compress</span>(cubehelix)
p2 &lt;-<span class="st"> </span><span class="kw">colorRampPalette</span>(cubebasis)(<span class="dv">255</span>)
<span class="kw">pal.bands</span>(p1, cubebasis, p2,
  <span class="dt">labels=</span><span class="kw">c</span>(<span class="st">'cubehelix(255)'</span>, <span class="st">'cubebasis'</span>,<span class="st">'expanded'</span>), <span class="dt">main=</span><span class="st">&quot;compression of cubehelix&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.maxdist</span>(p1,p2) <span class="co"># 2.08</span></code></pre></div>
<pre><code>## [1] 2.337919</code></pre>
</div>
<div id="pal.csf" class="section level2">
<h2>pal.csf()</h2>
<p>Show a colormap with a Campbell-Robson Contrast Sensitivity Chart.</p>
<p>In a contrast sensitivity figure as drawn by this function, the spatial frequency increases from left to right and the contrast decreases from bottom to top. The bars in the figure appear taller in the middle of the image than at the edges, creating an upside-down “U” shape, which is the “contrast sensitivity function”. Your perception of this curve depends on the viewing distance.</p>
<div id="what-to-look-for-3" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Are the vertical bands visible across the full vertical axis?</li>
<li>Do the vertical bands blur together along the right side?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.csf</span>(parula, <span class="dt">main=</span><span class="st">&quot;parula&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.cube" class="section level2">
<h2>pal.cube()</h2>
<p>The palette is converted to RGB or LUV coordinates and plotted in a three-dimensional scatterplot. The LUV space is probably better, but it is easier to tweak colors by hand in RGB space.</p>
<div id="what-to-look-for-4" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>A good palette has colors that are spread somewhat uniformly in 3D.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co">#pal.cube(cubehelix)</span>
<span class="co">#pal.cube(polychrome())</span></code></pre></div>
<p><img src="" alt="cubehelix" /> <img src="" alt="polychrome" /></p>
</div>
</div>
<div id="pal.heatmap" class="section level2">
<h2>pal.heatmap()</h2>
<p>A random heatmap is generated (with 5% missing values) and a key is added to the heatmap by appending a blank column along the right side and then a column with the palette colors.</p>
<div id="what-to-look-for-5" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Can the value of each cell be correctly inferred using the key on the right side?</li>
<li>Can missing values be identified?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">op &lt;-<span class="st"> </span><span class="kw">par</span>(<span class="dt">mfrow=</span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">2</span>), <span class="dt">mar=</span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">2</span>))
<span class="kw">pal.heatmap</span>(alphabet, <span class="dt">n=</span><span class="dv">26</span>, <span class="dt">main=</span><span class="st">&quot;alphabet&quot;</span>)
<span class="kw">pal.heatmap</span>(alphabet2, <span class="dt">n=</span><span class="dv">26</span>, <span class="dt">main=</span><span class="st">&quot;alphabet2&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">par</span>(op)</code></pre></div>
</div>
</div>
<div id="pal.map" class="section level2">
<h2>pal.map()</h2>
<p>Display a palette on a choropleth map similar to the ColorBrewer website.</p>
<div id="what-to-look-for-6" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Are regions distinct from each other?</li>
<li>Are county outlines visible?</li>
<li>Are outliers identifiable within each region?</li>
<li>Are colors identifiable in the complex area (lower left) part of the map?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.map</span>(brewer.paired, <span class="dt">n=</span><span class="dv">12</span>, <span class="dt">main=</span><span class="st">&quot;brewer.paired&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.safe" class="section level2">
<h2>pal.safe()</h2>
<p>A single palette/colormap is shown as five colored bands:</p>
<ol style="list-style-type: decimal">
<li>Without any modifications.</li>
<li>As seen in black-and-white as if photocopied.</li>
<li>As seen by the deutan color-blind.</li>
<li>As seen by the protan color-blind.</li>
<li>As seen by the tritan color-blind.</li>
</ol>
<div id="what-to-look-for-7" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Are colors still unique when viewed in less-than full color?</li>
<li>Is a sequential colormap still sequential?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.safe</span>(parula, <span class="dt">main=</span><span class="st">&quot;parula&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.scatter" class="section level2">
<h2>pal.scatter()</h2>
<p>Show a colormap with a scatterplot</p>
<div id="what-to-look-for-8" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Can the colors of each point be uniquely identified?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.scatter</span>(polychrome, <span class="dt">n=</span><span class="dv">36</span>, <span class="dt">main=</span><span class="st">&quot;alphabet&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.sineramp" class="section level2">
<h2>pal.sineramp()</h2>
<p>The test image shows a sine wave superimposed on a ramp of the palette. The amplitude of the sine wave is dampened/modulated from full at the top of the image to 0 at the bottom.</p>
<div id="what-to-look-for-9" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Along the top edge, is the sine wave equally visible across the entire image?</li>
<li>Along the bottom edge, is the ramp smooth, or are there vertical bands?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.sineramp</span>(parula, <span class="dt">main=</span><span class="st">&quot;parula&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<p>In the example below, the <code>jet</code> colormap fails both tests, the <code>tol.rainbow</code> colormap fails to clearly show the sinewave in the green/orange region.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">op &lt;-<span class="st"> </span><span class="kw">par</span>(<span class="dt">mfrow=</span><span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">1</span>), <span class="dt">mar=</span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">1</span>))
<span class="kw">pal.sineramp</span>(jet, <span class="dt">main=</span><span class="st">&quot;jet&quot;</span>)
<span class="kw">pal.sineramp</span>(tol.rainbow, <span class="dt">main=</span><span class="st">&quot;tol.rainbow&quot;</span>)
<span class="kw">pal.sineramp</span>(kovesi.rainbow, <span class="dt">main=</span><span class="st">&quot;kovesi.rainbow&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">par</span>(op)</code></pre></div>
</div>
</div>
<div id="pal.test" class="section level2">
<h2>pal.test()</h2>
<p>This function combines several other functions into a single test image.</p>
<p>The examples below show the superiority of the <code>parula</code> colormap as compared to the <code>viridis</code> colormap.</p>
<div id="what-to-look-for-10" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Are all squares (especially upper left and lower right) in the Z-curve distinct?</li>
<li>Are the contrast sensitivity function bands visible?</li>
<li>Is the sinewave showing bands of similar height, and smoothly varying along the bottom?</li>
<li>Is the actual peak of the volcano identifiable? The lowest area?</li>
<li>Is the gray/luminosity channel monotonic?</li>
</ol>
<p>The examples below show the poor performance of the ‘viridis’ colormap in dark regions. The ‘parula’ palette shows more structure in the volcano.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.test</span>(parula)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.test</span>(viridis) <span class="co"># dark colors are poor</span></code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.volcano" class="section level2">
<h2>pal.volcano()</h2>
<p>Some palettes with dark colors at one end of the palette hide the shape of the volcano in the dark colors.</p>
<div id="what-to-look-for-11" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>Can you locate the exact position of the highest point on the volcano?</li>
<li>Are the upper-right and lower-right corners the same elevation?</li>
<li>Do any Mach bands circle the peak?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.volcano</span>(parula)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.volcano</span>(viridis)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="pal.zcurve" class="section level2">
<h2>pal.zcurve()</h2>
<p>Show a Z-order curve, coloring cells with a colormap. The difference in color between squares side-by-side is 1/48 of the full range. The difference in color between one square atop another is 1/96 the full range.</p>
<div id="what-to-look-for-12" class="section level3">
<h3>What to look for:</h3>
<ol style="list-style-type: decimal">
<li>A good color palette of 64 colors should be able to resolve 4 sub-squares within each of the 16 squares.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.zcurve</span>(parula, <span class="dt">main=</span><span class="st">&quot;parula&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
</div>
<div id="using-palettes-with-ggplot2" class="section level1">
<h1>Using palettes with ggplot2</h1>
<p>To use any colormap with the <code>ggplot2</code> package, use the <code>ggplot2::scale_fill_gradientn()</code> function.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">require</span>(ggplot2)</code></pre></div>
<pre><code>## Loading required package: ggplot2</code></pre>
<pre><code>## 
## Attaching package: 'ggplot2'</code></pre>
<pre><code>## The following object is masked from 'package:latticeExtra':
## 
##     layer</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">require</span>(pals)
<span class="kw">require</span>(reshape2)</code></pre></div>
<pre><code>## Loading required package: reshape2</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="kw">melt</span>(volcano), <span class="kw">aes</span>(<span class="dt">x=</span>Var1, <span class="dt">y=</span>Var2, <span class="dt">fill=</span>value)) <span class="op">+</span><span class="st"> </span>
<span class="st">  </span><span class="kw">geom_tile</span>() <span class="op">+</span><span class="st"> </span>
<span class="st">  </span><span class="kw">scale_fill_gradientn</span>(<span class="dt">colours=</span><span class="kw">coolwarm</span>(<span class="dv">100</span>), <span class="dt">guide =</span> <span class="st">&quot;colourbar&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
<div id="catalog-of-colormaps-and-palettes" class="section level1">
<h1>Catalog of colormaps and palettes</h1>
<p>The following images show bands for all the colormaps and palettes in the <code>pals</code> package, grouped in</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Discrete</span>
<span class="kw">pal.bands</span>(alphabet, alphabet2, cols25, glasbey, kelly, okabe, polychrome, stepped, tol, watlington,
          <span class="dt">main=</span><span class="st">&quot;Discrete&quot;</span>, <span class="dt">show.names=</span><span class="ot">FALSE</span>)</code></pre></div>
<pre><code>## Only 26 colors are available with 'alphabet'</code></pre>
<pre><code>## Only 26 colors are available with 'alphabet2'</code></pre>
<pre><code>## Only 25 colors are available with 'cols25'.</code></pre>
<pre><code>## Only 32 colors are available with 'glasbey'.</code></pre>
<pre><code>## Only 22 colors are available with 'kelly'.</code></pre>
<pre><code>## Only 36 colors are available with 'polychrome'.</code></pre>
<pre><code>## Only 24 colors are available with 'stepped'</code></pre>
<pre><code>## Only 12 colors are available with 'tol'</code></pre>
<pre><code>## Only 16 colors are available with 'watlington'.</code></pre>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Misc</span>
<span class="kw">pal.bands</span>(coolwarm,warmcool,cubehelix,gnuplot,jet,parula,tol.rainbow,cividis)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Niccoli</span>
<span class="kw">pal.bands</span>(cubicyf,cubicl,isol,linearl,linearlhot,
          <span class="dt">main=</span><span class="st">&quot;Niccoli&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Qualtitative</span>
<span class="kw">pal.bands</span>(<span class="kw">brewer.accent</span>(<span class="dv">8</span>), <span class="kw">brewer.dark2</span>(<span class="dv">8</span>), <span class="kw">brewer.paired</span>(<span class="dv">12</span>), <span class="kw">brewer.pastel1</span>(<span class="dv">9</span>),
          <span class="kw">brewer.pastel2</span>(<span class="dv">8</span>), <span class="kw">brewer.set1</span>(<span class="dv">9</span>), <span class="kw">brewer.set2</span>(<span class="dv">8</span>), <span class="kw">brewer.set3</span>(<span class="dv">10</span>),
          <span class="dt">labels=</span><span class="kw">c</span>(<span class="st">&quot;brewer.accent&quot;</span>, <span class="st">&quot;brewer.dark2&quot;</span>, <span class="st">&quot;brewer.paired&quot;</span>, <span class="st">&quot;brewer.pastel1&quot;</span>,
                   <span class="st">&quot;brewer.pastel2&quot;</span>, <span class="st">&quot;brewer.set1&quot;</span>, <span class="st">&quot;brewer.set2&quot;</span>, <span class="st">&quot;brewer.set3&quot;</span>),
          <span class="dt">main=</span><span class="st">&quot;Brewer qualitative&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Sequential</span>
<span class="kw">pal.bands</span>(brewer.blues, brewer.bugn, brewer.bupu, brewer.gnbu, brewer.greens,
          brewer.greys, brewer.oranges, brewer.orrd, brewer.pubu, brewer.pubugn,
          brewer.purd, brewer.purples, brewer.rdpu, brewer.reds, brewer.ylgn,
          brewer.ylgnbu, brewer.ylorbr, brewer.ylorrd,
          <span class="dt">main=</span><span class="st">&quot;Brewer sequential&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Diverging</span>
<span class="kw">pal.bands</span>(brewer.brbg, brewer.piyg, brewer.prgn, brewer.puor, brewer.rdbu,
          brewer.rdgy, brewer.rdylbu, brewer.rdylgn, brewer.spectral,
          <span class="dt">main=</span><span class="st">&quot;Brewer diverging&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Ocean</span>
<span class="kw">pal.bands</span>(ocean.thermal, ocean.haline, ocean.solar, ocean.ice, ocean.gray,
          ocean.oxy, ocean.deep, ocean.dense, ocean.algae, ocean.matter,
          ocean.turbid, ocean.speed, ocean.amp, ocean.tempo, ocean.phase,
          ocean.balance, ocean.delta, ocean.curl, <span class="dt">main=</span><span class="st">&quot;Ocean&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Matplotlib</span>
<span class="kw">pal.bands</span>(magma, inferno, plasma, viridis, <span class="dt">main=</span><span class="st">&quot;Matplotlib&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Kovesi</span>
op =<span class="st"> </span><span class="kw">par</span>(<span class="dt">mar=</span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">10</span>,<span class="dv">2</span>,<span class="dv">1</span>))
<span class="kw">pal.bands</span>(kovesi.cyclic_grey_15_85_c0, kovesi.cyclic_grey_15_85_c0_s25,
kovesi.cyclic_mrybm_35_75_c68, kovesi.cyclic_mrybm_35_75_c68_s25,
kovesi.cyclic_mygbm_30_95_c78, kovesi.cyclic_mygbm_30_95_c78_s25,
kovesi.cyclic_wrwbw_40_90_c42, kovesi.cyclic_wrwbw_40_90_c42_s25,
kovesi.diverging_isoluminant_cjm_75_c23, kovesi.diverging_isoluminant_cjm_75_c24,
kovesi.diverging_isoluminant_cjo_70_c25, kovesi.diverging_linear_bjr_30_55_c53,
kovesi.diverging_linear_bjy_30_90_c45, kovesi.diverging_rainbow_bgymr_45_85_c67,
kovesi.diverging_bkr_55_10_c35, kovesi.diverging_bky_60_10_c30,
kovesi.diverging_bwr_40_95_c42, kovesi.diverging_bwr_55_98_c37,
kovesi.diverging_cwm_80_100_c22, kovesi.diverging_gkr_60_10_c40,
kovesi.diverging_gwr_55_95_c38, kovesi.diverging_gwv_55_95_c39,
kovesi.isoluminant_cgo_70_c39, kovesi.isoluminant_cgo_80_c38,
kovesi.isoluminant_cm_70_c39, kovesi.rainbow_bgyr_35_85_c72, kovesi.rainbow_bgyr_35_85_c73,
kovesi.rainbow_bgyrm_35_85_c69, kovesi.rainbow_bgyrm_35_85_c71,
<span class="dt">main=</span><span class="st">&quot;Kovesi&quot;</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">pal.bands</span>(kovesi.linear_bgy_10_95_c74,
kovesi.linear_bgyw_15_100_c67, kovesi.linear_bgyw_15_100_c68,
kovesi.linear_blue_5_95_c73, kovesi.linear_blue_95_50_c20,
kovesi.linear_bmw_5_95_c86, kovesi.linear_bmw_5_95_c89,
kovesi.linear_bmy_10_95_c71, kovesi.linear_bmy_10_95_c78,
kovesi.linear_gow_60_85_c27, kovesi.linear_gow_65_90_c35,
kovesi.linear_green_5_95_c69, kovesi.linear_grey_0_100_c0,
kovesi.linear_grey_10_95_c0, kovesi.linear_kry_5_95_c72,
kovesi.linear_kry_5_98_c75, kovesi.linear_kryw_5_100_c64,
kovesi.linear_kryw_5_100_c67, kovesi.linear_ternary_blue_0_44_c57,
kovesi.linear_ternary_green_0_46_c42, kovesi.linear_ternary_red_0_50_c52,
<span class="dt">main=</span><span class="st">&quot;Kovesi linear&quot;</span>
)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">par</span>(op)


<span class="co"># Bivariate</span>
bivcol &lt;-<span class="st"> </span><span class="cf">function</span>(pal){
  tit &lt;-<span class="st"> </span><span class="kw">substitute</span>(pal)
  pal &lt;-<span class="st"> </span><span class="kw">pal</span>()
  ncol &lt;-<span class="st"> </span><span class="kw">length</span>(pal)
  nx &lt;-<span class="st"> </span><span class="kw">sqrt</span>(<span class="kw">length</span>(pal))
  <span class="kw">image</span>(<span class="kw">matrix</span>(<span class="dv">1</span><span class="op">:</span>ncol, <span class="dt">nrow=</span><span class="kw">sqrt</span>(ncol)), <span class="dt">axes=</span><span class="ot">FALSE</span>, <span class="dt">col=</span>pal)
  <span class="kw">mtext</span>(tit)
}
op &lt;-<span class="st"> </span><span class="kw">par</span>(<span class="dt">mfrow=</span><span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">4</span>), <span class="dt">mar=</span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">1</span>))
<span class="kw">bivcol</span>(stevens.pinkgreen)
<span class="kw">bivcol</span>(stevens.bluered)
<span class="kw">bivcol</span>(stevens.pinkblue)
<span class="kw">bivcol</span>(stevens.greenblue)
<span class="kw">bivcol</span>(stevens.purplegold)
<span class="kw">bivcol</span>(brewer.orangeblue)
<span class="kw">bivcol</span>(brewer.pinkblue)
<span class="kw">bivcol</span>(tolochko.redblue)
<span class="kw">bivcol</span>(arc.bluepink)
<span class="kw">bivcol</span>(census.blueyellow)
<span class="kw">par</span>(op)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>



<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
